package com.dupengcheng.wx.entity;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class WXMessageTypeFactory  implements InitializingBean {


    @Resource
    private List<WXMessageTypeHandler>  handlers;


    private Map<MessageTypeEnum,WXMessageTypeHandler> typeMap=new HashMap<>();

    public WXMessageTypeHandler getTypeHandlerByType(String type){
        MessageTypeEnum byType = MessageTypeEnum.getByType(type);
        WXMessageTypeHandler wxMessageTypeHandler = typeMap.get(byType);
        if (!ObjectUtils.isEmpty(wxMessageTypeHandler)){
            return wxMessageTypeHandler;
        }
        return null;

    }


    @Override
    public void afterPropertiesSet() throws Exception {

        for (WXMessageTypeHandler handler : handlers) {
            typeMap.put(handler.getType(),handler);
        }
    }
}
